package com.ywt.test4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ShowChaptersIframe extends JFrame {
    private  NovelDownloaderIframe novelDownloaderIframe;//上一个页面的对象
    private Novel novel;//当前的小说对象
    JPanel bottomPanel;//显示所有图书数据的总布局
    //显示所有图书数据的总布局
    //显示所有章节的布局容器
    JPanel chapterPanel;
    int page = 1;
    //所有章节信息
    ArrayList<Chapter> chapters;
    //总页数
    int allPages;


    public ShowChaptersIframe(Novel novel, NovelDownloaderIframe novelDownloaderIframe) throws HeadlessException {
        this.novel = novel;
        this.novelDownloaderIframe = novelDownloaderIframe;
        initData();//获取数据
        init();
        this.setVisible(true);//显示
    }

    /**
     * 获取小说所有的章节
     */
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //获取所有章节信息
                    chapters = NovelUtils.getChapters(novel.getUrl());
                    //计算总页数
                    allPages = (int) (Math.ceil(chapters.size() / 20.0));
                    //根据信息 刷新布局
                    freshChaptersPanel(chapters);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }).start();
    }

    private void init() {
        this.setTitle("小说下载器");
        this.setSize(400, 600);
        this.setLocationRelativeTo(null);//居中
        this.setResizable(false);//禁止缩放
        this.setLayout(new FlowLayout());

        //显示所有章节的布局容器
        chapterPanel = new JPanel();
        chapterPanel.setPreferredSize(new Dimension(350, 500));
        chapterPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));

        JPanel pagePanel = new JPanel();
        pagePanel.setPreferredSize(new Dimension(350, 100));
        pagePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 5));
        JButton preBtn = new JButton("上一页");
        JButton nextBtn = new JButton("下一页");
        JButton backBtn = new JButton("返回");
        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //当前页面销毁
                ShowChaptersIframe.this.dispose();
                //显示上一个页面
                novelDownloaderIframe.setVisible(true);
            }
        });
        preBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("pre" + page);
                page--;
                if (chapters == null || page <= 0) {
                    page++;
                    return;
                }
                // 1000   2
                //page 0     1       2
                //    20*0-20*0+19  20*1-20*1+19  20*2 - 20*2+19
                //截取当前页的所有数据
                int start = 20 * (page - 1);
                int end = 20 * (page - 1) + 20;
                if (end > chapters.size()) {
                    end = chapters.size();
                }
                List<Chapter> pageChapters = chapters.subList(start, end);
                //刷新页面数据
                freshChaptersPanel(pageChapters);
            }
        });
        //page 2                      3
        nextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                page++;
                System.out.println(page + "next" + allPages);
                if (chapters != null && page > allPages) {
                    page--;
                    return;
                }
                // 65   2
                //page 0     1       2
                //  20*0-20*0+19  20*1-20*1+19  20*2 - 20*2+19   20*3 - 20*3+19
                //截取当前页的所有数据
                int start = 20 * (page - 1);
                int end = 20 * (page - 1) + 20;
                if (end > chapters.size()) {
                    end = chapters.size();
                }
                List<Chapter> pageChapters = chapters.subList(start, end);
                //刷新页面数据
                freshChaptersPanel(pageChapters);
            }
        });
        pagePanel.add(backBtn);
        pagePanel.add(preBtn);
        pagePanel.add(nextBtn);

        this.add(chapterPanel);
        this.add(pagePanel);
    }


    /**
     * 刷新章节的布局
     */
    private void freshChaptersPanel(List<Chapter> chapters) {
        if (chapters == null || chapters.size() == 0) {
            return;
        }
        chapterPanel.removeAll();
        for (int i = 0; i < chapters.size(); i++) {
            Chapter chapter = chapters.get(i);
            JPanel jPanel = new JPanel();
            JLabel jLabel = new JLabel(chapter.getName());
            jPanel.setPreferredSize(new Dimension(350, 20));
            jPanel.setBackground(Color.PINK);
            jPanel.add(jLabel);
            jPanel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    //打开小说内容的界面 传入 参1 小说对象 参2 章节对象
                    // 参3 当前的页面的对象
                    new ShowDetailIframe(novel, chapter, ShowChaptersIframe.this);
                    //隐藏当前页面
                    ShowChaptersIframe.this.setVisible(false);
                }
            });
            chapterPanel.add(jPanel);
        }
        chapterPanel.updateUI();
        System.out.println(chapters);
    }
}
